﻿using System;
using System.Collections.Generic;
using System.Text;
using X.DataFactory;
using X.Entity;
using X.Common;

namespace X.Bll
{
    public sealed class User
    {
        private static readonly IUser dal = DataAccess.CreateUser();

        public static bool Create(UserInfo userInfo)
        {
            userInfo.Password = Cryptography.CreateDbPassword(userInfo.Password);
            return dal.Create(userInfo);
        }

        public static bool Delete(int userId)
        {
            return dal.Delete(userId);
        }

        public static UserInfo GetUser(int userId)
        {
            return dal.GetUser(userId);
        }

        public static UserInfo GetUser(string loginName)
        {
            return dal.GetUser(loginName);
        }

        public static IList<UserInfo> GetUserList()
        {
            return dal.GetUserList();
        }

        public static bool Exists(string loginName)
        {
            return dal.Exists(loginName);
        }

        public static UserInfo AuthenticateUser(string loginName, byte[] password)
        {
            UserInfo userInfo = GetUser(loginName);

            if (null != userInfo)
            {
                if (!Cryptography.ComparePasswords(userInfo.Password, password))
                {
                    return new UserInfo(true);
                }
            }

            return userInfo;
        }

        public static bool ChangePassword(int userId, byte[] password)
        {
            password = Cryptography.CreateDbPassword(password);
            return dal.ChangePassword(userId, password);
        }
    }
}
